using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace ICP_MS;

public class FormLogin : Form
{
	private string[] lngfiles = null;

	private Hashtable _regionHash = new Hashtable();

	private Hashtable _fileHash = new Hashtable();

	private LastUser LastUserInfo = new LastUser();

	private IContainer components = null;

	private Label label1;

	private Label label2;

	private TextBox txtUser;

	private TextBox txtPSW;

	private GroupBox groupBox1;

	private Button button1;

	private Button button2;

	private ErrorProvider loginError;

	private ComboBox cbLang;

	private Label lblLang;

	public FormLogin()
	{
		InitializeComponent();
		_regionHash.Add("en", "English");
		_regionHash.Add("Ru", "русский язык");
		_regionHash.Add("zh-cn", "简体中文");
		_regionHash.Add("zh-CHS", "简体中文");
		_regionHash.Add("zh-TW", "正体中文");
		_regionHash.Add("de", "Deutsch");
	}

	private void FormLogin_Load(object sender, EventArgs e)
	{
		string dbPath = Application.StartupPath + "\\iMassConfig\\analyteLib.db3";
		DataBase.Instance.InitDB(dbPath);
		UserManager.Instance.LoadUsers();
		LoadLngfiles();
		LoadLastInfo();
		if (LastUserInfo != null && LastUserInfo.lastUser != null && LastUserInfo.lastUser != "")
		{
			txtUser.Text = LastUserInfo.lastUser;
		}
		else
		{
			txtUser.Text = "User";
		}
		if (LastUserInfo != null && LastUserInfo.lastLang != null)
		{
			if (LastUserInfo.lastLang != "")
			{
				cbLang.SelectedItem = LastUserInfo.lastLang;
			}
		}
		else if (cbLang.Items.Count > 0)
		{
			cbLang.SelectedIndex = 0;
		}
		ApplyResources();
	}

	private void LoadLastInfo()
	{
		IList<LastUser> LastUserInfos = DataBase.Instance.LoadLastUser();
		if (LastUserInfos == null || LastUserInfos.Count == 0)
		{
			LastUserInfo.lastUser = "User";
		}
		else
		{
			LastUserInfo = LastUserInfos[0];
		}
	}

	private void button2_Click(object sender, EventArgs e)
	{
		loginError.Clear();
		string name = txtUser.Text;
		name.Trim();
		string psw = txtPSW.Text;
		psw.Trim();
		if (UserManager.Instance.GetUserInfo(name, psw))
		{
			UserManager.Instance.OnUserChanged();
			LastUserInfo.lastUser = UserManager.CurrentUser.name;
			LastUserInfo.lastLang = cbLang.Text;
			DataBase.Instance.SaveLastUser(LastUserInfo);
			base.DialogResult = DialogResult.OK;
		}
		else
		{
			loginError.SetError(txtPSW, Lang.Txt("FormLogin.Login.Error.Text"));
		}
		base.DialogResult = DialogResult.OK;
	}

	private void LoadLngfiles()
	{
		cbLang.Items.Clear();
		lngfiles = Enumerate("*.txt");
		Array.Sort(lngfiles);
		try
		{
			string[] array = lngfiles;
			foreach (string filename in array)
			{
				if (!filename.Contains("Lang"))
				{
					continue;
				}
				string country = filename;
				string domain = null;
				int index = filename.LastIndexOf('.');
				if (index == -1)
				{
					if (filename == "Lang")
					{
						domain = "en";
					}
				}
				else
				{
					domain = filename.Substring(index + 1);
				}
				if (_regionHash.Contains(domain))
				{
					country = _regionHash[domain] as string;
					cbLang.Items.Add(country + "[" + domain + "]");
				}
			}
		}
		catch
		{
		}
	}

	private void LoadResHash(string filename)
	{
		try
		{
			Lang.ResHash.Clear();
			using (StreamReader sr = new StreamReader(filename))
			{
				while (!sr.EndOfStream)
				{
					string line = sr.ReadLine();
					if (line.IndexOf('=') != -1)
					{
						string[] result = line.Split('=');
						Lang.ResHash.Add(result[0], result[1]);
					}
				}
			}
			if (Lang.ResHash.Count < 100)
			{
				LoadDefault();
			}
		}
		catch
		{
			LoadDefault();
		}
	}

	private void LoadDefault()
	{
		string orgFile = Application.StartupPath + "\\default.tmp";
		Lang.CreateDefault(orgFile);
		if (File.Exists(orgFile))
		{
			Lang.Culture = "";
			LoadResHash(orgFile);
		}
	}

	public string[] Enumerate(string filter)
	{
		_fileHash.Clear();
		string[] files = Directory.GetFiles(Application.StartupPath ?? "", filter);
		ArrayList results = new ArrayList();
		for (int i = 0; i < files.Length; i++)
		{
			string name = Path.GetFileNameWithoutExtension(files[i]);
			_fileHash.Add(name, files[i]);
			results.Add(name);
		}
		return (string[])results.ToArray(typeof(string));
	}

	protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
	{
		switch (keyData)
		{
		case Keys.Return:
			button2.PerformClick();
			break;
		case Keys.Escape:
			button1.PerformClick();
			break;
		}
		return base.ProcessCmdKey(ref msg, keyData);
	}

	private void button1_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
	}

	private void cbLang_SelectedIndexChanged(object sender, EventArgs e)
	{
		int index = cbLang.SelectedIndex;
		string text = cbLang.Text;
		int index2 = text.IndexOf('[') + 1;
		int index3 = text.IndexOf(']');
		Lang.Culture = text.Substring(index2, index3 - index2);
		LoadResHash(_fileHash[lngfiles[index]] as string);
		ApplyResources();
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormLogin.Title.Text") + "(" + $"V{Assembly.GetExecutingAssembly().GetName().Version.ToString()}" + ")";
		label1.Text = Lang.Txt("FormLogin.Label.User.Text");
		label2.Text = Lang.Txt("FormLogin.Label.Password.Text");
		button2.Text = Lang.Txt("FormLogin.Button.OK.Text");
		button1.Text = Lang.Txt("FormLogin.Button.Cancel.Text");
		lblLang.Text = Lang.Txt("FormLogin.Label.Language.Text");
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ICP_MS.FormLogin));
		this.label1 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.txtUser = new System.Windows.Forms.TextBox();
		this.txtPSW = new System.Windows.Forms.TextBox();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.cbLang = new System.Windows.Forms.ComboBox();
		this.lblLang = new System.Windows.Forms.Label();
		this.button1 = new System.Windows.Forms.Button();
		this.button2 = new System.Windows.Forms.Button();
		this.loginError = new System.Windows.Forms.ErrorProvider(this.components);
		this.groupBox1.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.loginError).BeginInit();
		base.SuspendLayout();
		this.label1.Location = new System.Drawing.Point(41, 33);
		this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(150, 21);
		this.label1.TabIndex = 0;
		this.label1.Text = "用户：";
		this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		this.label2.Location = new System.Drawing.Point(41, 77);
		this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(150, 21);
		this.label2.TabIndex = 0;
		this.label2.Text = "密码：";
		this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		this.txtUser.Location = new System.Drawing.Point(208, 32);
		this.txtUser.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.txtUser.Name = "txtUser";
		this.txtUser.Size = new System.Drawing.Size(221, 22);
		this.txtUser.TabIndex = 1;
		this.txtPSW.Location = new System.Drawing.Point(208, 76);
		this.txtPSW.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.txtPSW.Name = "txtPSW";
		this.txtPSW.Size = new System.Drawing.Size(221, 22);
		this.txtPSW.TabIndex = 2;
		this.txtPSW.UseSystemPasswordChar = true;
		this.groupBox1.Controls.Add(this.cbLang);
		this.groupBox1.Controls.Add(this.lblLang);
		this.groupBox1.Controls.Add(this.txtPSW);
		this.groupBox1.Controls.Add(this.txtUser);
		this.groupBox1.Controls.Add(this.label2);
		this.groupBox1.Controls.Add(this.label1);
		this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
		this.groupBox1.Location = new System.Drawing.Point(0, 0);
		this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.groupBox1.Size = new System.Drawing.Size(495, 169);
		this.groupBox1.TabIndex = 2;
		this.groupBox1.TabStop = false;
		this.cbLang.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.cbLang.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbLang.FormattingEnabled = true;
		this.cbLang.Location = new System.Drawing.Point(208, 123);
		this.cbLang.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.cbLang.Name = "cbLang";
		this.cbLang.Size = new System.Drawing.Size(221, 24);
		this.cbLang.TabIndex = 7;
		this.cbLang.SelectedIndexChanged += new System.EventHandler(cbLang_SelectedIndexChanged);
		this.lblLang.Location = new System.Drawing.Point(41, 124);
		this.lblLang.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.lblLang.Name = "lblLang";
		this.lblLang.Size = new System.Drawing.Size(150, 21);
		this.lblLang.TabIndex = 6;
		this.lblLang.Text = "语言：";
		this.lblLang.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		this.button1.Location = new System.Drawing.Point(283, 193);
		this.button1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.button1.Name = "button1";
		this.button1.Size = new System.Drawing.Size(79, 36);
		this.button1.TabIndex = 4;
		this.button1.Text = "取消";
		this.button1.UseVisualStyleBackColor = true;
		this.button1.Click += new System.EventHandler(button1_Click);
		this.button2.Location = new System.Drawing.Point(112, 193);
		this.button2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.button2.Name = "button2";
		this.button2.Size = new System.Drawing.Size(79, 36);
		this.button2.TabIndex = 3;
		this.button2.Text = "确定";
		this.button2.UseVisualStyleBackColor = true;
		this.button2.Click += new System.EventHandler(button2_Click);
		this.loginError.ContainerControl = this;
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 16f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(495, 257);
		base.Controls.Add(this.button2);
		base.Controls.Add(this.button1);
		base.Controls.Add(this.groupBox1);
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		base.Name = "FormLogin";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
		this.Text = "用户登录";
		base.Load += new System.EventHandler(FormLogin_Load);
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		((System.ComponentModel.ISupportInitialize)this.loginError).EndInit();
		base.ResumeLayout(false);
	}
}
